﻿// Page3.cpp : 实现文件
//

#include "stdafx.h"
#include "DmTool.h"
#include "Page3.h"
#include "afxdialogex.h"
#include "CppSQLite3.h"

// CPage3 对话框

IMPLEMENT_DYNAMIC(CPage3, CDialogEx)

CPage3::CPage3(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_PAGE3, pParent)
{

}

CPage3::~CPage3()
{
}

void CPage3::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_lstOcrBase);
	DDX_Control(pDX, IDC_CMB_PAGE3_OCRDETAIL, m_cmbOcrDetail);
	DDX_Control(pDX, IDC_LIST2, m_lstOcrTask);
}


BEGIN_MESSAGE_MAP(CPage3, CDialogEx)
	ON_BN_CLICKED(IDC_BTN_PAGE3_BASESAVE, &CPage3::OnBnClickedBtnPage3Basesave)
	ON_LBN_SELCHANGE(IDC_LIST1, &CPage3::OnLbnSelchangeList1)
	ON_LBN_DBLCLK(IDC_LIST1, &CPage3::OnLbnDblclkList1)
	ON_CBN_SELCHANGE(IDC_CMB_PAGE3_OCRDETAIL, &CPage3::OnCbnSelchangeCmbPage3Ocrdetail)
	ON_BN_CLICKED(IDC_BTN_PAGE3_TASKSAVE, &CPage3::OnBnClickedBtnPage3Tasksave)
	ON_BN_CLICKED(IDC_BUTTON1, &CPage3::OnBnClickedButton1)
END_MESSAGE_MAP()


// CPage3 消息处理程序

void CPage3::ReLoad() {
	m_lstOcrBase.ResetContent();
	m_cmbOcrDetail.ResetContent();
	m_lstOcrTask.DeleteAllItems();
	CppSQLite3DB db;
	CFileFind fFind;
	if (fFind.FindFile(_T("DMTool.db")))
	{

		db.open("DMTool.db", "ppo00123");
		CppSQLite3Query query = db.execQuery("SELECT * FROM ocrBase order by type='1'");
		//CMap<CString, CString, OcrInfo, OcrInfo> ocrCmp1;
		int i = 0;
		ocrBase.clear();
		while (!query.eof())
		{
			int index = m_lstOcrBase.AddString(Str(query.fieldValue("name")));
			ItemValue* item = new ItemValue;
			item->id = query.fieldValue("id");
			item->name = query.fieldValue("name");
			m_lstOcrBase.SetItemData(index, (DWORD)item);
			ocrBase[Str(query.fieldValue("id"))] = query.fieldValue("name");
			//delete item;
			query.nextRow();
		}

		query = db.execQuery("SELECT * FROM ocrdetail ");

		while (!query.eof())
		{

			int index = m_cmbOcrDetail.AddString(Str(query.fieldValue("name")));
			ItemValue* item = new ItemValue;
			item->id = query.fieldValue("id");
			item->name = query.fieldValue("name");
			m_cmbOcrDetail.SetItemData(index, (DWORD)item);
			//delete item;


			//ocrBase.insert(query.fieldValue("id"), query.fieldValue("name"));
			query.nextRow();
		}

		query = db.execQuery("SELECT * FROM ocrTask ");
		while (!query.eof())
		{

			int nIndex = m_lstOcrTask.GetItemCount();
			m_lstOcrTask.InsertItem(nIndex, Str(query.fieldValue("id")));
			int nI = 1;

			m_lstOcrTask.SetItemText(nIndex, nI++, ocrBase[Str(query.fieldValue("taskID"))]);
			m_lstOcrTask.SetItemText(nIndex, nI++, Str(query.fieldValue("ocrDetail")));
			query.nextRow();

		}

	}
}



void CPage3::OnBnClickedBtnPage3Basesave()
{
	// TODO: 在此添加控件通知处理程序代码
	CppSQLite3DB db;
	CFileFind fFind;
	if (fFind.FindFile(_T("DMTool.db")))
	{

		db.open("DMTool.db", "ppo00123");
		CString str;
		GetDlgItemText(IDC_EDT_PAGE3_BASENAME, str);
		if (str == "") {
			AfxMessageBox(L"请输入名称");
			return;
		}
		CString sql;
		//sql.Format(L"insert into ocrbase(name,type) values('%s')", str);
		sql = L"insert into ocrbase(name,type) values('" + str + L"',0)";
		//const char* s= "insert into ocrbase(name,type) values('测试',0)";
		int nAffected = db.execDML(StrP(sql));
		SetDlgItemText(IDC_EDT_PAGE3_BASENAME, L"");
		ReLoad();
	}

}


void CPage3::OnLbnSelchangeList1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	m_lstOcrBase.GetText(m_lstOcrBase.GetCurSel(), str);
	SetDlgItemText(IDC_EDT_PAGE3_BASENAME1, str);
	ItemValue* item = (ItemValue*)m_lstOcrBase.GetItemData(m_lstOcrBase.GetCurSel());
	m_ocrBaseID = item->id;
}

void CPage3::OnLbnDblclkList1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	m_lstOcrBase.GetText(m_lstOcrBase.GetCurSel(), str);
	SetDlgItemText(IDC_EDT_PAGE3_BASENAME, str);
	ItemValue* item = (ItemValue*)m_lstOcrBase.GetItemData(m_lstOcrBase.GetCurSel());
	m_ocrBaseID = item->id;
}


void CPage3::OnCbnSelchangeCmbPage3Ocrdetail()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	m_cmbOcrDetail.GetLBText(m_cmbOcrDetail.GetCurSel(), str);


	//m_lstOcrBase.GetText(m_lstOcrBase.GetCurSel(), str);
	//SetDlgItemText(IDC_EDT_PAGE3_BASENAME1, str);
	if (!clist.Find(str)) {
		clist.AddTail(str);
	}
	else {
		return;
	}
	int i = 0;
	POSITION pos = clist.GetTailPosition();
	CString cstr;
	while (i<clist.GetCount())
	{
		CString m;
		if (i == 0) {
			cstr += clist.GetAt(pos);
		}
		else {
			//str += "," + cList.GetAt(pos);
			m = clist.GetAt(pos);
			cstr = m + "," + cstr;
		}
		i++;
		clist.GetPrev(pos);
		//cList.GetNext(pos);
	}
	SetDlgItemText(IDC_EDT_PAGE3_MODUL, cstr);
}


void CPage3::OnBnClickedBtnPage3Tasksave()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str, smodul;
	GetDlgItemText(IDC_EDT_PAGE3_BASENAME1, str);
	if (str == L"") {
		AfxMessageBox(L"请选择任务");
		return;
	}
	GetDlgItemText(IDC_EDT_PAGE3_MODUL, smodul);
	if (smodul == L"") {
		AfxMessageBox(L"请选择模块");
		return;
	}

	CppSQLite3DB db;
	CFileFind fFind;
	if (fFind.FindFile(_T("DMTool.db")))
	{

		db.open("DMTool.db", "ppo00123");
		CString str;
		GetDlgItemText(IDC_EDT_PAGE3_BASENAME, str);

		CString sql;
		sql = L"insert into ocrTask(taskID,ocrDetail) values('" + m_ocrBaseID + L"','" + smodul + L"')";
		int nAffected = db.execDML(StrP(sql));
		SetDlgItemText(IDC_EDT_PAGE3_BASENAME, L"");
		ReLoad();
	}
}


BOOL CPage3::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	int nIndex = 0;

	m_lstOcrTask.InsertColumn(nIndex++, _T("编号"), LVCFMT_LEFT, 50);
	m_lstOcrTask.InsertColumn(nIndex++, _T("任务名称"), LVCFMT_LEFT, 100);
	m_lstOcrTask.InsertColumn(nIndex++, _T("模块名称"), LVCFMT_LEFT, 400);
	m_lstOcrTask.SetBkColor(RGB(205, 226, 252));
	m_lstOcrTask.SetTextBkColor(RGB(205, 226, 252));
	m_lstOcrTask.SetTextColor(RGB(0, 0, 255));
	m_lstOcrTask.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CPage3::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	AfxMessageBox(L"tt");
}
